<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\MiniProgram;
use App\Constants\MiniProgramConstant;
use App\Services\MiniProgramService;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\JsonResponse;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;

class MiniProgramController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new MiniProgram(), function (Grid $grid) {
            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->model()->whereIn('slug', array_keys($map))->orderByDesc('id');

            $grid->column('id');
            $grid->column('name');
            $grid->column('slug');
            $grid->column('status')->using(['否', '是'])->label(['danger', 'primary']);;
            $grid->column('created_at');

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append('<a style="margin-right:5px" href="' . admin_route('mini_program_setting.index', ['slug' => $actions->row->slug]) . '">参数配置</a>');
                $actions->append('<a style="margin-right:5px" href="' . admin_route('mini_program_special.index', ['mini_type' => $actions->row->slug]) . '">装修配置</a>');
                $actions->append('<a style="margin-right:5px" href="' . admin_route('mini_program_msg_board.index', ['mini_type' => $actions->row->slug]) . '">客服</a>');
                $actions->append('<a style="margin-right:5px" href="' . admin_route('diy_form.index', ['mini_type' => $actions->row->slug]) . '">自定义表单</a>');
            });

            $grid->disableBatchDelete();
            $grid->disableDeleteButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('id')->width(2);
                $filter->equal('status')->select([0 => '禁用', 1 => '正常'])->width(2);
                $filter->equal('slug')->select(MiniProgramConstant::MAP)->width(2);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new MiniProgram(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('slug');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new MiniProgram(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
//            $form->select('slug')->required()->options(MiniProgramConstant::MAP);
            $form->text('slug')->required();
            $form->radio('status')->options(['禁用', '正常'])->default(0);

            $form->submitted(function (Form $form) {
                $id = $form->getKey();
                $slug = $form->slug;

                $exists = \App\Models\MiniProgram::where('slug', '=', $slug)
                    ->when($id, function($query, $id){
                        return $query->where('id', '!=', $id);
                    })->exists();

                if ($exists) {
                    $form->responseValidationMessages('slug', '已存在');
                }
            });

            $form->saved(function (Form $form, $result){
                if ($form->isCreating() && $result) {
                    app(MiniProgramService::class)->initSetting($form->slug);
                }
                Cache::forget(MiniProgramConstant::CACHE_KEY);
            });

            $form->deleted(function (Form $form, $result){
                if ($result) {
                    $slugs = Arr::pluck($form->model()->toArray(), 'slug');
                    \app(MiniProgramService::class)->deleteSetting($slugs);
                }
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
